﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCTask.Models;

namespace MVCTask.Controllers
{
    public class CategoryController : Controller
    {
        private TaskContext db = new TaskContext();

        //
        // GET: /Category/
        [Authorize]
        public ViewResult Index()
        {
            return View(db.Categories.Where(t => t.User == User.Identity.Name).ToList());
        }

        public ActionResult getJSON()
        {
            var jsonCategories = from m in db.Categories
                                 select new
                                 {
                                     m.ID,
                                     m.Description,
                                     Tasks = new List<Task>(
                                         (from c in m.Tasks
                                          select new Task()
                                          {
                                              ID = c.ID,
                                              Description = c.Description
                                          }).Cast<Task>())
                                 };
            return Json(jsonCategories, JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /Category/Details/5
        [Authorize]
        public ViewResult Details(int id)
        {
            Category category = db.Categories.Find(id);
            return View(category);
        }

        //
        // GET: /Category/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Category/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                category.User = User.Identity.Name;
                db.Categories.Add(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(category);
        }

        //
        // GET: /Category/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
            Category category = db.Categories.Find(id);
            return View(category);
        }

        //
        // POST: /Category/Edit/5
        [Authorize]
        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                category.User = User.Identity.Name;
                db.Entry(category).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(category);
        }

        //
        // GET: /Category/Delete/5
        [Authorize]
        public ActionResult Delete(int id)
        {
            Category category = db.Categories.Find(id);
            return View(category);
        }

        //
        // POST: /Category/Delete/5
        [Authorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Find(id);
            db.Categories.Remove(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}